﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestTsConverter1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            // 创建 OpenFileDialog 对象
            OpenFileDialog openFileDialog = new OpenFileDialog();

            // 设置允许选择多个文件
            openFileDialog.Multiselect = true;

            // 设置对话框的标题
            openFileDialog.Title = "选择文件";

            // 设置默认文件类型筛选器
            openFileDialog.Filter = "All files (*.ts)|*.ts";

            // 显示对话框
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Clear();

                if (openFileDialog.FileNames.Length > 0)
                {

                    // 用户点击了确定按钮
                    foreach (string file in openFileDialog.FileNames)
                    {
                        // 处理选择的文件，例如显示文件名
                        //MessageBox.Show(file);

                        listBox1.Items.Add(file);
                    }
                    convertBtn.Visible = true;
                }
            }
            else
            {
                convertBtn.Visible = false;
            }
        }

        private async void ConvertBtn_Click(object sender, EventArgs e)
        {
            convertBtn.Enabled = false;  // 禁用转换按钮
            chooseFilesBtn.Enabled = false;  // 禁用选择文件按钮
            convertBtn.Text = "正在转换...";  // 修改转换按钮的文本为"正在转换..."


            // 创建临时列表存储 listBox1 中的文件名
            List<string> fileList = new List<string>();
            foreach (string item in listBox1.Items)
            {
                fileList.Add(item);
            }

            foreach (string file in fileList)
            {
                string newFile = Path.ChangeExtension(file, ".mp4");
                string command = $@"-y -i ""{file}"" -c copy -bsf:a aac_adtstoasc ""{newFile}""";
                string exePath = ExtractResource("TestTsConverter1.ffmpeg.exe", "ffmpeg.exe");

                await Task.Run(() => Execute(exePath, command));

                // 删除 listBox1 中的文件名
                listBox1.Items.Remove(file);

                // 删除本地文件
                if (DeleteFlag.Checked)
                {
                    File.Delete(file);
                }
            }

            // 转换完成后恢复按钮状态
            convertBtn.Enabled = true;  // 启用转换按钮
            chooseFilesBtn.Enabled = true;  // 启用选择文件按钮
            convertBtn.Text = "开始转换";  // 恢复转换按钮的文本为"转换"

            // 显示提示框
            MessageBox.Show("转换完成！", "转换完成", MessageBoxButtons.OK, MessageBoxIcon.Information);

            // 播放提示声音
            //System.Media.SystemSounds.Asterisk.Play();
        }

        private static void Execute(string exePath, string parameters)
        {
            using (Process p = new Process())
            {
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.FileName = exePath;
                p.StartInfo.Arguments = parameters;
                p.Start();
                p.WaitForExit();
            }
        }

        private string ExtractResource(string resourceName, string outputFileName)
        {
            string outputPath = Path.Combine(Path.GetTempPath(), outputFileName);
            if (!File.Exists(outputPath))
            {
                using (Stream resourceStream = GetType().Assembly.GetManifestResourceStream(resourceName))
                {
                    if (resourceStream != null)
                    {
                        using (FileStream fileStream = new FileStream(outputPath, FileMode.Create))
                        {
                            resourceStream.CopyTo(fileStream);
                        }
                    }
                }
            }
            return outputPath;
        }
    }
}
